package neusoft.yuejian_11.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import neusoft.yuejian_11.activity.RegActivity;
import neusoft.yuejian_11.base.BaseFragment;
import neusoft.yuejian_11.bean.SampleLogin;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.MainActivity;
import neusoft.yuejian_11.model.UserModel;
import neusoft.yuejian_11.R;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
import static neusoft.yuejian_11.R.id.editText_user;


/**
 * A simple {@link Fragment} subclass.
 */
//用户登录（已改）
public class LoginFragment extends BaseFragment implements RetrofitListener {
    private EditText editTextUser;
    private EditText editTextPass;
    private Button button_log;
    private TextView zhuce;

    public LoginFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        editTextUser = (EditText) view.findViewById(editText_user);
        editTextPass = (EditText) view.findViewById(R.id.editText_pass);
        button_log = (Button) view.findViewById(R.id.button_log);
        zhuce= (TextView) view.findViewById(R.id.newperson);
        initEvents();//点击事件


    }

    private void initEvents() {
        //开始登陆
        button_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel userModel = new UserModel();
                String phones =editTextUser.getText().toString();
                String contents = editTextPass.getText().toString();
                userModel.login(phones,contents,LoginFragment.this);
            }
        });
        //点击注册新用户链接
        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(getActivity(),RegActivity.class);
                startActivityForResult(intent,1 );//requestCode
            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                //启动注册activity对应上面的数字
                switch (requestCode){

                    case RESULT_OK:
                        String userName=data.getStringExtra("username");
                        editTextUser.setText(userName);
                        break;
                    case RESULT_CANCELED:
                        break;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_login, container, false);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        SampleLogin sampleLogin=(SampleLogin) o;
        if (sampleLogin.getU_id()==0){
            Toast.makeText(getActivity(),"用户名或密码错误",Toast.LENGTH_LONG);
        } else {
            Toast.makeText(getActivity(),"登录成功",Toast.LENGTH_LONG);
            MainActivity mainActivity = (MainActivity) getActivity();
            if (mainActivity != null){
                mainActivity.replaceLoginFragment();//显示主页面
                mainActivity.setSavedUserId(sampleLogin.getU_id());//保存文件中
                setU_id(sampleLogin.getU_id());//保存到程序中
            }
        }
    }

    @Override
    public void onFail() {

    }

}
